package com.bobo.dao.photo;

import com.bobo.dao.BaseDao;
import com.bobo.model.Photo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by bobo on 2016/10/17.
 */
public class PhotoDaoImpl extends BaseDao<Photo> implements IPhotoDao{
    @Override
    public Integer create(Photo photo) {

        String sql = "INSERT INTO t_photo(`img`, status) VALUES(?, ?)";

        return create(sql, photo.getImg(), photo.getStatus());
    }

    @Override
    public boolean update(Photo photo) {
        String sql = "UPDATE t_photo p SET p.`img`=? , p.status = ?  WHERE p.id=?";
        return update(sql, photo.getImg(), photo.getStatus(), photo.getId());
    }

    @Override
    public Photo getById(Integer id) {
        String sql = "SELECT id,img,status FROM t_photo WHERE id=?";
        Photo photo = new Photo();
        ResultSet rs =  getObject(sql, id);
        try {
            while(rs.next()){
                photo.setId(id);
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return photo;
    }



    @Override
    public List<Photo> getPhotos(Integer current, Integer pageSize, Object... obj) {
        StringBuilder sql = new StringBuilder("SELECT id,img,status, upload_date date FROM t_photo where 1 = 1");

            sql.append(" and status in (");

            for(int i = 0; i < obj.length; i++) {
                sql.append(" ?, ");
            }
            sql.delete(sql.lastIndexOf(","), sql.length());

            sql.append(")");



        List<Photo> photos = new ArrayList<>();
        ResultSet rs = pager(sql.toString(),current,pageSize, obj);
        try {
            while(rs.next()){
                Photo photo = new Photo();
                photo.setId(rs.getInt("id"));
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("status"));
                photo.setTime(rs.getTimestamp("date"));
                photos.add(photo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return photos;
    }

    @Override
    public List<Photo> clearPhoto() {

        String sql = "select id, img from t_photo  where status = ? and   upload_date  between ? and ?";
        List<Photo> photos = new ArrayList<>();
        ResultSet rs = getObject(sql, -1,  new Timestamp(System.currentTimeMillis() - 48 * 3600 * 1000), new Timestamp(System.currentTimeMillis() - 24 * 3600*1000) );
        try {
            while(rs.next()){
                Photo photo = new Photo();
                photo.setId(rs.getInt("id"));

                photos.add(photo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return photos;
    }

//    @Override
//    public Integer getCount(Integer status) {
//        return this.getCount("where status = ?  ", status);
//    }
    @Override
    public Integer getCount(Object... obj) {

        StringBuilder sql = new StringBuilder("where 1 = 1 ");
        sql.append("and status in (");

        for(int i = 0; i < obj.length; i++) {
            sql.append(" ?, ");
        }
        sql.delete(sql.lastIndexOf(","), sql.length());

        sql.append(")");

        return this.getCount(sql.toString(), obj);


    }


}
